package com.zusmart.base.buffer.support;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import com.zusmart.base.buffer.Buffer;
import com.zusmart.base.util.Assert;

public class ByteBufferBuffer extends AbstractBuffer {

	private static final ByteBuffer EMPTY_DCONTENT = ByteBuffer.allocateDirect(0);
	private static final ByteBuffer EMPTY_HCONTENT = ByteBuffer.allocate(0);

	public static ByteBufferBuffer allocate(int capacity, boolean direct) {
		Assert.isNull(capacity < 0, "capacity must >= 0");
		return new ByteBufferBuffer(direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity), 0, capacity);
	}

	public static ByteBufferBuffer wrap(ByteBuffer buffer) {
		ByteBufferBuffer result = new ByteBufferBuffer(buffer.duplicate(), 0, buffer.capacity());
		result.limit(buffer.limit()).position(buffer.position());
		result.setReadOnly(buffer.isReadOnly());
		result.setBigEndian(buffer.order() == ByteOrder.BIG_ENDIAN);
		return result;
	}

	private ByteBuffer content;

	protected ByteBufferBuffer(ByteBuffer content, int offset, int capacity) {
		super(offset, capacity);
		this.content = content;
	}

	@Override
	public boolean isDirect() {
		return this.content.isDirect();
	}

	@Override
	public Buffer compact() {
		this.checkReadOnly();
		ByteBuffer duplicate = this.content.duplicate();
		duplicate.position(this.getIndex(0));
		duplicate.limit(duplicate.position() + this.remaining());
		this.content.position(this.getIndex(0, 0));
		this.content.put(duplicate);
		this.position(this.remaining()).limit(this.capacity());
		return this;
	}

	@Override
	public Buffer slice() {
		return new DelegateBuffer(this.getIndex(0), this.remaining()).setReadOnly(this.isReadOnly());
	}

	@Override
	public Buffer duplicate() {
		ByteBufferBuffer buffer = new DelegateBuffer(this.getIndex(0, 0), this.capacity());
		buffer.limit(this.limit()).position(this.position());
		buffer.mark(this.getMark());
		buffer.setReadOnly(this.isReadOnly());
		return buffer;
	}

	@Override
	public ByteBuffer asByteBuffer() {
		int limit = this.content.limit();
		this.content.position(getIndex(0, 0));
		this.content.limit(this.content.position() + this.capacity());
		ByteBuffer buffer = this.content.slice();
		buffer.position(this.position());
		buffer.limit(limit());
		this.content.limit(limit);
		return this.isReadOnly() ? buffer.asReadOnlyBuffer() : buffer;
	}

	@Override
	protected byte doGet(int index) {
		return this.content.get(index);
	}

	@Override
	protected void doPut(int index, byte b) {
		this.content.put(index, b);
	}

	@Override
	protected void doRelease() {
		this.content = this.isDirect() ? EMPTY_DCONTENT : EMPTY_HCONTENT;
	}

	@Override
	public int write(WritableByteChannel channel) throws IOException {
		this.checkReadOnly();
		int count = 0;
		int limit = this.content.limit();
		int position = this.getIndex(0);
		this.content.position(position).limit(position + this.remaining());
		try {
			count = channel.write(content);
		} finally {
			content.limit(limit);
			skip(count);
		}
		return count;
	}

	@Override
	public int read(ReadableByteChannel channel) throws IOException {
		int count = 0;
		int limit = this.content.limit();
		try {
			int position = this.getIndex(0);
			this.content.position(position).limit(position + this.remaining());
			count = channel.read(this.content);
		} finally {
			this.content.limit(limit);
			if (count > 0) {
				skip(count);
			}
		}
		return count;
	}

	@Override
	public Buffer get(byte[] dst, int offset, int length) {
		checkBounds(offset, length, dst.length);
		this.content.position(this.getIndex(length));
		this.content.get(dst, offset, length);
		return this;
	}

	@Override
	public Buffer get(int index, byte[] dst, int offset, int length) {
		checkBounds(offset, length, dst.length);
		this.content.position(this.getIndex(index, length));
		this.content.get(dst, offset, length);
		return this;
	}

	@Override
	public Buffer get(ByteBuffer dst, int length) {
		checkBounds(0, length, dst.remaining());
		this.content.position(this.getIndex(length));
		int limit = this.content.limit();
		this.content.limit(this.content.position() + length);
		dst.put(this.content);
		this.content.limit(limit);
		return this;
	}

	@Override
	public Buffer get(int index, ByteBuffer dst, int length) {
		checkBounds(0, length, dst.remaining());
		this.content.position(this.getIndex(index, length));
		int limit = this.content.limit();
		this.content.limit(this.content.position() + length);
		dst.put(this.content);
		this.content.limit(limit);
		return this;
	}

	@Override
	public Buffer get(Buffer dst, int length) {
		checkBounds(0, length, dst.remaining());
		this.content.position(this.getIndex(length));
		dst.put(this.content, length);
		return this;
	}

	@Override
	public Buffer get(int index, Buffer dst, int length) {
		checkBounds(0, length, dst.remaining());
		this.content.position(this.getIndex(index, length));
		dst.put(this.content, length);
		return this;
	}

	@Override
	public Buffer put(byte[] src, int offset, int length) {
		checkBounds(offset, length, src.length);
		this.content.position(this.putIndex(length));
		this.content.put(src, offset, length);
		return this;
	}

	@Override
	public Buffer put(int index, byte[] src, int offset, int length) {
		checkBounds(offset, length, src.length);
		this.content.position(this.putIndex(index, length));
		this.content.put(src, offset, length);
		return this;
	}

	@Override
	public Buffer put(ByteBuffer src, int length) {
		checkBounds(0, length, src.remaining());
		this.content.position(this.putIndex(length));
		int limit = src.limit();
		src.limit(src.position() + length);
		this.content.put(src);
		src.limit(limit);
		return this;
	}

	@Override
	public Buffer put(int index, ByteBuffer src, int length) {
		checkBounds(0, length, src.remaining());
		this.content.position(this.putIndex(index, length));
		int limit = src.limit();
		src.limit(src.position() + length);
		this.content.put(src);
		src.limit(limit);
		return this;
	}

	@Override
	public Buffer put(Buffer src, int length) {
		checkBounds(0, length, src.remaining());
		this.content.position(this.putIndex(length));
		src.get(this.content, length);
		return this;
	}

	@Override
	public Buffer put(int index, Buffer src, int length) {
		checkBounds(0, length, src.remaining());
		this.content.position(this.putIndex(index, length));
		src.get(this.content, length);
		return this;
	}

	@Override
	public char getChar() {
		return this.content.getChar(this.getIndex(2));
	}

	@Override
	public char getChar(int index) {
		return this.content.getChar(this.getIndex(index, 2));
	}

	@Override
	public Buffer putChar(char c) {
		this.content.putChar(this.putIndex(2), c);
		return this;
	}

	@Override
	public Buffer putChar(int index, char c) {
		content.putChar(this.putIndex(index, 2), c);
		return this;
	}

	@Override
	public short getShort() {
		return this.content.getShort(this.getIndex(2));
	}

	@Override
	public short getShort(int index) {
		return this.content.getShort(this.getIndex(index, 2));
	}

	@Override
	public Buffer putShort(short s) {
		this.content.putShort(this.putIndex(2), s);
		return this;
	}

	@Override
	public Buffer putShort(int index, short s) {
		this.content.putShort(this.putIndex(index, 2), s);
		return this;
	}

	@Override
	public int getInt() {
		return this.content.getInt(this.getIndex(4));
	}

	@Override
	public int getInt(int index) {
		return this.content.getInt(this.getIndex(index, 4));
	}

	@Override
	public Buffer putInt(int i) {
		this.content.putInt(this.putIndex(4), i);
		return this;
	}

	@Override
	public Buffer putInt(int index, int i) {
		this.content.putInt(this.putIndex(index, 4), i);
		return this;
	}

	@Override
	public long getLong() {
		return this.content.getLong(this.getIndex(8));
	}

	@Override
	public long getLong(int index) {
		return this.content.getLong(this.getIndex(index, 8));
	}

	@Override
	public Buffer putLong(long l) {
		this.content.putLong(this.putIndex(8), l);
		return this;
	}

	@Override
	public Buffer putLong(int index, long l) {
		this.content.putLong(this.putIndex(index, 8), l);
		return this;
	}

	@Override
	public float getFloat() {
		return this.content.getFloat(this.getIndex(4));
	}

	@Override
	public float getFloat(int index) {
		return this.content.getFloat(this.getIndex(index, 4));
	}

	@Override
	public Buffer putFloat(float f) {
		this.content.putFloat(this.putIndex(4), f);
		return this;
	}

	@Override
	public Buffer putFloat(int index, float f) {
		this.content.putFloat(this.putIndex(index, 4), f);
		return this;
	}

	@Override
	public double getDouble() {
		return this.content.getDouble(this.getIndex(8));
	}

	@Override
	public double getDouble(int index) {
		return this.content.getDouble(this.getIndex(index, 8));
	}

	@Override
	public Buffer putDouble(double d) {
		this.content.putDouble(this.putIndex(8), d);
		return this;
	}

	@Override
	public Buffer putDouble(int index, double d) {
		this.content.putDouble(this.putIndex(index, 8), d);
		return this;
	}

	private class DelegateBuffer extends ByteBufferBuffer {

		public DelegateBuffer(int offset, int capacity) {
			super(content, offset, capacity);
		}

		@Override
		public boolean isReleased() {
			return ByteBufferBuffer.this.isReleased();
		}

		@Override
		public void release() {
			ByteBufferBuffer.this.release();
		}

		@Override
		public boolean isPermanent() {
			return ByteBufferBuffer.this.isPermanent();
		}

		@Override
		public Buffer setPermanent(boolean b) {
			return ByteBufferBuffer.this.setPermanent(b);
		}

	}

}